//
//  HMMainViewController.swift
//  WB25
//
//  Created by HM on 2017/6/28.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 自定义tabBar
        let tabBar = HMTabBar()
        
        
        // 设置代理
        tabBar.hmDelegate = self
        // 02闭包实例化
        tabBar.closure = {
            // 04 回调
            print("监听按钮点击")
        }
        // 使用kvc 完成只读属性赋值
        setValue(tabBar, forKey: "tabBar")
        
        addChildViewController(childController: HMHomeViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(childController: HMMessageViewController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(childController: HMDiscoverViewController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(childController: HMProfileViewController(), title: "我", imgName: "tabbar_profile")
    }
    
    
    /// 添加字控制器公共方法
    ///
    /// - Parameters:
    ///   - childController: 字控制器
    ///   - title: 名字
    ///   - imgName: 图片名
    func addChildViewController(childController: UIViewController, title: String, imgName: String) {

        // 设置title
        childController.navigationItem.title = title
        childController.tabBarItem.title = title
        // 选中的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: UIControlState.selected)
        
        // 设置image
        childController.tabBarItem.image = UIImage(named: imgName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        // 选中
        // .withRenderingMode(UIImageRenderingMode.alwaysOriginal) 图片渲染
        childController.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        // 导航控制器
        let nav = HMNavigationController(rootViewController: childController)
        
        // 添加子控制器
        addChildViewController(nav)
    }

}

extension HMMainViewController: HMTabBarDelegate{
    func composeButtonSelect() {
        print("按钮点击代理")
        HMComposeView.show(target: self)
    }
}
